/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Login.java
 *
 * Created on 2009-12-1, 22:08:10
 */
package com.ordering.login;

import com.ordering.dao.UserManageJpaController;
import com.ordering.menu.OrderManage;
import com.ordering.menu.UserManageEdit;
import com.ordering.pojo.UserManage;
import com.ordering.utils.WindowLocationUtils;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import org.jvnet.substance.SubstanceLookAndFeel;

/**
 *
 * @author Administrator
 */
public class Login extends javax.swing.JFrame {

    /** Creates new form Login */
    public Login() {
            initComponents();
            javax.swing.JFrame.setDefaultLookAndFeelDecorated(true);
            JDialog.setDefaultLookAndFeelDecorated(true);

            SubstanceLookAndFeel.setSkin(new org.jvnet.substance.skin.BusinessSkin());
//            UIManager.setLookAndFeel("ch.randelshofer.quaqua.QuaquaLookAndFeel");
            WindowLocationUtils.setWindowCenter(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtUserName = new javax.swing.JTextField();
        txtPassword = new javax.swing.JPasswordField();
        btnLogin = new javax.swing.JButton();
        btnQuit = new javax.swing.JButton();
        bott = new javax.swing.JLabel();
        btnRegist = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("点 菜 系 统 登 录"); // NOI18N

        jLabel1.setText("用 户 名:");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText("登录密码:");
        jLabel2.setName("jLabel2"); // NOI18N

        txtUserName.setName("txtUserName"); // NOI18N

        txtPassword.setName("txtPassword"); // NOI18N

        btnLogin.setText("登录");
        btnLogin.setName("btnLogin"); // NOI18N
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        btnQuit.setText("退出");
        btnQuit.setName("btnQuit"); // NOI18N
        btnQuit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuitActionPerformed(evt);
            }
        });

        bott.setText("    提 示 :  请输入用户名和密码");
        bott.setName("bott"); // NOI18N

        btnRegist.setText("注册");
        btnRegist.setName("btnRegist"); // NOI18N
        btnRegist.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegistActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(bott, javax.swing.GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(78, 78, 78)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(4, 4, 4)
                        .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnLogin)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(4, 4, 4)
                                .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(19, 19, 19)
                                .addComponent(btnQuit)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnRegist)))))
                .addGap(95, 95, 95))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {txtPassword, txtUserName});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel2});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtUserName, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtPassword))
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnQuit)
                    .addComponent(btnLogin)
                    .addComponent(btnRegist))
                .addGap(30, 30, 30)
                .addComponent(bott)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        this.validateUser();
    }//GEN-LAST:event_btnLoginActionPerformed

    private void btnQuitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQuitActionPerformed
        this.quit();
    }//GEN-LAST:event_btnQuitActionPerformed

    private void btnRegistActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegistActionPerformed
       UserManageEdit editWin = new UserManageEdit();
       editWin.init();
       editWin.setVisible(true);
    }//GEN-LAST:event_btnRegistActionPerformed

    private void validateUser() {
        String userName = txtUserName.getText();
        String password = txtPassword.getText();
        UserManageJpaController jpa = new UserManageJpaController();

        final UserManage um = jpa.findUserManageByUserId(userName);

        try {
            if (um != null) { // 用户名存在
                if(!um.getDelmark()){
                   bott.setText(clue + "无权限用户，请和管理员联系 ...");
                }else if (password.equals(um.getPwd())) {
                    bott.setText(clue + "登录成功，正在进入系统 ...");
                    java.awt.EventQueue.invokeLater(new Runnable() {

                        public void run() {
                            OrderManage om = new OrderManage(um);
                            om.setVisible(true);
                        }
                    });
                    this.setVisible(false);
                } else {
                    bott.setText(clue + "用户 [ " + userName + " ] 的密码不正确，请重新输入 ...");
                    flag++;
                    if (flag == 3) { // 三次密码验证
                        JOptionPane.showMessageDialog(null,
                                "您不是本系统的管理员，系统关闭 ...", "警告",
                                JOptionPane.ERROR_MESSAGE);
                        System.exit(0);
                    }// End if(flag == 3)
                    return;
                }// End if(pwd.equals(rs.getString(1)))
            } else {
                bott.setText(clue + "用户ID [ " + userName + " ] 不存在 ...");
            }// End if(rs.next())
        } catch (Exception ex) {
            ex.printStackTrace();
        }// End try
    }

    private void quit() {
        int flag = 0;
        String msg = "您 现 在 要 关 闭 系 统 吗 ？";
        flag = JOptionPane.showConfirmDialog(null, msg, "提示",
                JOptionPane.YES_NO_OPTION);
        if (flag == JOptionPane.YES_OPTION) {
            this.setVisible(false);
            System.exit(0);
        }// End if(flag == JOptionPane.YES_OPTION)
        return;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Login().setVisible(true);
            }
        });
    }
    String clue = "    提 示 :  ";
    int flag = 0; // 登记次数记数器
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel bott;
    private javax.swing.JButton btnLogin;
    private javax.swing.JButton btnQuit;
    private javax.swing.JButton btnRegist;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUserName;
    // End of variables declaration//GEN-END:variables
}
